package cn.cnic.security.nssap.stearm.etl.handler;

import cn.cnic.security.nssap.stearm.etl.dao.sandboxDao;
import cn.cnic.security.nssap.stearm.etl.entity.sandboxEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.List;

/**
 * 文件威胁鉴定器
 * Created by xiaojianping
 * Data:2021/6/16
 * Time:15:57
 */
public class sandboxAlarmHandler implements AlarmHandler<sandboxEntity> {
    public sandboxEntity process(String jsonOriginal){
        sandboxEntity list= JSON.parseObject(jsonOriginal, sandboxEntity.class);
        return  list;

    }

    @Override
    public int insertDB(sandboxEntity entity) {
        sandboxDao sa=new sandboxDao();
        int count=sa.add(entity);
        return count;
    }

    @Override
    public void batchInsert(List<sandboxEntity> list) {

    }

    @Override
    public void prepareStatementBatch(String json, PreparedStatement alarmStmt, String sql) throws SQLException {
        JSONObject map = JSON.parseObject(json);

        Long first_access_time = map.getLong("first_access_time");
        alarmStmt.setTimestamp(1,new Timestamp(first_access_time));

        Byte hazard_level = map.getByte("hazard_level");
        alarmStmt.setByte(2,hazard_level);

        String type_chain = map.getString("type_chain");
        alarmStmt.setString(3,type_chain);

        String attck  = map.getString("attck");
        alarmStmt.setString(4,attck);

        Integer repeat_count  = map.getInteger("repeat_count");
        alarmStmt.setInt(5,repeat_count);

        String uri = map.getString("uri");
        alarmStmt.setString(6,uri);

        String x_forwarded_for = map.getString("x_forwarded_for");
        if(x_forwarded_for !=null && x_forwarded_for.length() > 0){
            alarmStmt.setString(7,x_forwarded_for);
        }else{
            alarmStmt.setNull(7,Types.VARCHAR);
        }

        JSONArray rsp_status  = map.getJSONArray("rsp_status");
        //数组，用逗号分隔保存到一列
        if(rsp_status !=null && rsp_status.size() > 0){
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < rsp_status.size(); i++) {
                sb.append(rsp_status.getString(i));
                if(rsp_status.size() - i != 1) { //最后一次不加逗号
                    sb.append(",");
                }
            }

            alarmStmt.setString(8,sb.toString());
        }else{
            alarmStmt.setNull(8,Types.VARCHAR);
        }

        Integer alarm_source = map.getInteger("alarm_source");
        if(alarm_source != null){
            alarmStmt.setInt(9,alarm_source);
        }else{
            alarmStmt.setNull(9, Types.INTEGER);
        }

        String alarm_sip = map.getString("alarm_sip");
        alarmStmt.setString(10,alarm_sip);

        String threat_name = map.getString("threat_name");
        alarmStmt.setString(11,threat_name);

        Integer group_id  = map.getInteger("group_id");
        if(group_id != null){
            alarmStmt.setInt(12,group_id);
        }else{
            alarmStmt.setNull(12,Types.INTEGER);
        }

        String attack_org = map.getString("attack_org");
        if(attack_org != null && attack_org.length() > 0){
            alarmStmt.setString(13,attack_org);
        }else{
            alarmStmt.setNull(13,Types.VARCHAR);
        }

        String attack_sip  = map.getString("attack_sip");
        alarmStmt.setString(14,attack_sip);

        String host  = map.getString("host");
        alarmStmt.setString(15,host);

        Integer host_state  = map.getInteger("host_state");
        if(host_state != null){
            alarmStmt.setInt(16,host_state);
        }else{
            alarmStmt.setNull(16,Types.INTEGER);
        }

        String ioc = map.getString("ioc");
        alarmStmt.setString(17,ioc);

        Long access_time = map.getLong("access_time");
        alarmStmt.setTimestamp(18,new Timestamp(access_time));

        String attack_chain = map.getString("attack_chain");
        alarmStmt.setString(19,attack_chain);

        alarmStmt.addBatch();
    }
}
